# 神经网络 NeuralNetwork

from sklearn.datasets import load_digits # 引入 sklearn 库 手写数字训练集
from NeuralNetwork import NeuralNetwork


# 读入手写数字数据集
digits = load_digits()
# 初始化神经网络模型
layer_sizes = [64, 128, 64, 10]
print("神经网络模型初始化完毕！")
neunet = NeuralNetwork(layer_sizes)
# 采用小批量方式进行模型的训练
x_train = digits.data[:1500] # 训练图片
y_train = digits.target[:1500] # 训练图片对应的数字
x_pred = digits.data[1500:] # 验证图片
y_pred = digits.target[1500:] # 验证图片对应的数字
print("----------\n开始训练神经网络模型...")
neunet.fit(x_train, y_train, 0.01, 100)
print("神经网络模型训练完毕！")
print("----------\n开始识别手写数字...")
label = neunet.predict(x_pred)
result = label
cost = neunet.cost_func(result, y_pred)
print("神经网络识别结果：", label)
print("测试集真实结果：", y_pred)
error_num = 0
for i in range(int(len(label))):
    if y_pred[i] != label[i]:
        error_num += 1
print("错误个数：%d"%error_num, "错误率：%.2f%%"%((error_num/len(label))*100))